<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Mail\OrderPost;
use App\Models\Order;
use App\Transformers\OrderTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class OrderController extends BaseController
{
    /**
     * 订单列表
     */
    public function index(Request $request)
    {
        //搜索条件
        $order_no = $request->input('order_no');//订单单号
        $trade_no = $request->input('trade_no');//支付单号
        $status = $request->input('status');//订单状态
        $express_no = $request->input('express_no');//快递单号
        $orders = Order::when($order_no,function ($query) use ($order_no){
            $query->where('order_no',$order_no);
        })
        ->when($trade_no,function ($query) use ($trade_no){
            $query->where('trade_no',$trade_no);
        })
        ->when($status,function ($query) use ($status){
            $query->where('status',$status);
        })
        ->when($express_no,function ($query) use ($express_no){
            $query->where('express_no',$express_no);
        })
        ->paginate(1);
        return $this->response->paginator($orders,new OrderTransformer());
    }
    /**
     * 订单详情
     */
    public function show(Order $order)
    {
        return $this->response->item($order,new OrderTransformer());
    }
    /**
     * 发货
     */
    public function post(Request $request,Order $order)
    {
        // //发货类型：哪家快递公司
        // $order->express_type = $request->input('express_type');
        // //快递单号
        // $order->express_no = $request->input('express_no');
        // //订单状态 3 发货
        // $order->status = 3;
        //
        // $order->save();
        //
        // //邮件发送：通知买家发货了
        //
        // Mail::to($order->user)->queue(new OrderPost($order));
        // 使用事件辅助函数分发
        //        event(new \App\Events\OrderPost(
        //            $order,
        //            $request->input('express_type'),
        //            $request->input('express_no')
        //        ));

        //使用事件分发
        \App\Events\OrderPost::dispatch(
            $order,
            $request->input('express_type'),
            $request->input('express_no')
        );

        return $this->response->noContent();
    }
}
